package Qieces;

import java.awt.*;

/**
 * 将类
 *
 */
public class General extends Qiece{

    public General(Qiece a,int index,int c){

        if (index == 0){
            this.kind = 1;
            this.x = a.x;
            this.y = a.y;
            this.controller = c;
            this.moveof = 0;
        }else if (index == 1){
            this.kind = a.kind;
            this.controller = a.controller;
        }
        this.scl = 0;
    }

    @Override
    public void paintSelf(Graphics g) {
        if(this.controller == 0 && this.kind !=0){           //为黑子时
            g.drawImage(generals_b,x,y,null);
        } else if(this.controller == 1 && this.kind !=0){    //为红子时
            g.drawImage(generals_r,x,y,null);
        }
        if(this.moveof == 1){
            g.drawImage(point,x+10,y+10,null);
        }
    }

    @Override
    public void movingRange(Qiece[] qieces, int row, int col) {
        if(row<5){                          //黑将
            if(row<2 && (qieces[row*9+9+col].controller != this.controller || qieces[row*9+9+col].kind == 0)) {
                qieces[row*9+9+col].moveof = 1;
            }
            if(row>0 && (qieces[row*9-9+col].controller != this.controller || qieces[row*9-9+col].kind == 0)) {
                qieces[row*9-9+col].moveof = 1;
            }
            if(col<5 && (qieces[row*9+1+col].controller != this.controller || qieces[row*9+1+col].kind == 0)) {
                qieces[row * 9 + col + 1].moveof = 1;
            }
            if(col>3 && (qieces[row*9-1+col].controller != this.controller || qieces[row*9-1+col].kind == 0)) {
                qieces[row*9-1+col].moveof = 1;
            }
            //下
            for (int h = row+1; h*9+col < 90; h++){
                if (qieces[h*9+col].getKind() != 0){
                    if (qieces[h*9+col].getKind() != 1){
                        break;
                    }else {
                        qieces[h*9+col].setMoveof(1);
                    }
                }
            }

        }
        else{                               //红将
            if(row<9 && (qieces[row*9+9+col].controller != this.controller || qieces[row*9+9+col].kind == 0)) {
                qieces[row*9+9+col].moveof = 1;
            }
            if(row>7 && (qieces[row*9-9+col].controller != this.controller || qieces[row*9-9+col].kind == 0)) {
                qieces[row*9-9+col].moveof = 1;
            }
            if(col<5 && (qieces[row*9+1+col].controller != this.controller || qieces[row*9+1+col].kind == 0)) {
                qieces[row*9+col+1].moveof = 1;
            }
            if(col>3 && (qieces[row*9-1+col].controller != this.controller || qieces[row*9-1+col].kind == 0)) {
                qieces[row*9-1+col].moveof = 1;
            }
            //上
            for (int h = row-1; h*9+col >= 0; h--){
                if (qieces[h*9+col].getKind() != 0){
                    if (qieces[h*9+col].getKind() != 1){
                        break;
                    }else {
                        qieces[h*9+col].setMoveof(1);
                    }
                }
            }
        }
    }
}